<%@ page language="java" contentType="text/html; charset=utf-8"
		 pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
	<title>分类添加</title>
	<meta charset="utf-8" />
	<link rel="stylesheet" href="css/bootstrap.css" />
</head>
<body>
<div class="contain">


	<jsp:include page="/admin/header.jsp"></jsp:include>
		<div id="right_content">
			<a class="btn btn-warning" href="category.jsp">返回</a>
	<br><br>
	<form class="form-horizontal" action="/admin/user_add" method="post">
		<div class="form-group">
			<label for="input_name" class="col-sm-1 control-label">分类名称</label>
			<div class="col-sm-10">
				<input type="text" class="form-control" id="input_name" name="categoryName" placeholder="请输入分类名称">
			</div>
		</div>
		<div class="form-group">
			<label for="input_name" class="col-sm-1 control-label">父分类</label>
			<div class="col-sm-6">
				<select class="form-control" id="select_name" name="address">
				</select>
			</div>
		</div>

		<div class="form-group">
			<div class="col-sm-offset-1 col-sm-10">
				<button type="button" class="btn btn-success" id="submitButton">提交保存</button>
			</div>
		</div>
	</form>
	<span style="color:red;"></span>
		</div>
</div>
</div>
<script src="../static/ku/jQuery/jquery.min.js"></script>
<script type="text/javascript">
	getList();
	function getList() {
		$.get("/novel/category", {}, function (data) {
			console.log("Data received from server:", data); // 调试信息
			$('#select_name').empty();
			$('#select_name').append('<option value="">请选择分类</option>');
			$.each(data, function (index, category) {
				$('#select_name').append('<option value="' + category.id + '">' + category.categoryName + '</option>');
			});
		}).fail(function (xhr, status, error) {
			console.error("Error fetching categories: " + error); // 错误信息
		})
	}

	$('#submitButton').click(function () {
		var categoryName = $('#input_name').val();
		var parentId = $('#select_name').val();
		console.log(categoryName);
		console.log(parentId);
		if (!categoryName) {
			alert("分类名称不能为空");
			return;
		}
		$.ajax({
			url: "/novel/category",
			type: "POST",
			contentType: "application/json",
			data: JSON.stringify({ categoryName: categoryName, parentId: parentId }),
			success: function (data) {
				if (data === true) {
					alert("添加成功");
					location.reload();
				} else {
					alert("添加失败");
				}
			},
			error: function (xhr, status, error) {
				console.error("Error adding category: " + error); // 错误信息
			}
		});
	});
	// $.ajax({
	// 	type: 'POST',
	// 	url: '/novel/category',
	// 	data: {
	// 		categoryName: categoryName,
	// 		parentId: parentId
	// 	},
	// 	success: function (response) {
	// 		console.log("Category added successfully:", response); // 调试信息
	// 		$('#message').text('分类添加成功');
	// 		$('#categoryForm')[0].reset();
	// 		getList();
	// 	},
	// 	error: function (xhr, status, error) {
	// 		console.error("Error adding category: " + error); // 错误信息
	// 		$('#message').text('分类添加失败');
	// 	}
	// });
</script>
</body>
</html>
